package com.example.sea.core.data.user.service;

import com.example.sea.core.common.utils.Md5Util;
import com.example.sea.core.common.utils.UUIDUtil;
import com.example.sea.core.data.user.entity.AuthRole;
import com.example.sea.core.data.user.entity.AuthUser;
import com.example.sea.core.data.user.mapper.AuthRoleMapper;
import com.example.sea.core.data.user.mapper.AuthUserMapper;
import com.example.sea.core.data.user.model.AuthUserModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class AuthUserService {

    @Autowired
    private AuthUserMapper authUserMapper;

    @Autowired
    private AuthRoleMapper authRoleMapper;

    public AuthUser findByLogin(String account){
        Map params=new HashMap();
        params.put("account",account);
        List<AuthUser> authUsers = authUserMapper.selectListByParamsMap(params);
        if (CollectionUtils.isEmpty(authUsers)){
            return null;
        }else{
            return authUsers.get(0);
        }
    }

    public PageInfo<AuthUserModel> findPageByParams(Map params) {
        PageHelper.startPage(Integer.valueOf(params.get("pageNum").toString()), Integer.valueOf(params.get("pageSize").toString()));
        List<AuthUser> authUsers = authUserMapper.selectListByParamsMap(params);
        List<AuthUserModel> authUserModels=new ArrayList<>();
        for (AuthUser authUser:authUsers) {
            AuthRole authRole = authRoleMapper.selectByPrimaryKey(authUser.getRoleId());
            AuthUserModel authUserModel=new AuthUserModel();
            authUserModel.setId(authUser.getId());
            authUserModel.setAccount(authUser.getAccount());
            authUserModel.setPassword(authUser.getPassword());
            authUserModel.setState(authUser.getState());
            authUserModel.setCreateTime(authUser.getCreateTime());
            authUserModel.setAuthRole(authRole);
            authUserModels.add(authUserModel);
        }
        PageInfo<AuthUserModel> pageInfo = new PageInfo<>(authUserModels);
        return pageInfo;
    }

    public int add(AuthUser authUser){
        authUser.setId(UUIDUtil.getUUID32());
        authUser.setCreateTime(new Date());
        authUser.setPassword(Md5Util.getMd5(authUser.getPassword()));
        return authUserMapper.insert(authUser);
    }

    @Transactional
    public int delete(String[] ids){
        int count=0;
        for (String id:ids) {
            count += authUserMapper.deleteByPrimaryKey(id);
        }
        return count;
    }

    /**
     * 根据ID进行查询
     * @param id
     * @return
     */
    public AuthUser findById(String id){
        return authUserMapper.selectByPrimaryKey(id);
    }

    @Transactional
    public int update(AuthUser authUser){
        return authUserMapper.updateByPrimaryKey(authUser);
    }

}
